package com.mentaleye.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.mentaleye.bean.DeptRelationBean;
import com.mentaleye.dal.dao.DeptRelationDao;
import com.mentaleye.dal.model.DeptRelation;
import com.mentaleye.dal.model.DeptRelationExample;
import com.mentaleye.dal.model.DeptRelationKey;
import com.mentaleye.service.DeptRelationService;
import com.mentaleye.util.ObjectUtil;

@Service
public class DeptRelationServiceImpl implements DeptRelationService{
	
	@Autowired private DeptRelationDao deptRelationDao;
	
	@Override
	public boolean addDeptRelation(DeptRelationBean bean){
		if(bean != null){
			DeptRelation relation = (DeptRelation) ObjectUtil.transfer(bean, DeptRelation.class);
			return deptRelationDao.insertDeptRelation(relation);
		}
		return false;
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public boolean addDeptRelationList(List<DeptRelationBean> deptRelationBeanList){
		if(deptRelationBeanList != null){
			List<DeptRelation> list = ObjectUtil.transferList(deptRelationBeanList, DeptRelation.class);
			return deptRelationDao.insertDeptRelationList(list);
		}
		return false;
	}
	
	@Override
	public boolean removeDeptRelationOne(Integer deptId,Integer teacherId){
		if(deptId != null && teacherId != null){
			DeptRelationKey key = new DeptRelationKey();
			key.setDeptId(deptId);
			key.setTeacherId(teacherId);
			return deptRelationDao.deleteDeptRelation(key);
		}
		return false;
	}
	
	@Override
	public boolean removeDeptRelationList(Integer deptId,List<Integer> teacherids){
		Map<String, Object> map = new HashMap<String, Object>();
		map.put("deptId", deptId);
		map.put("teacherlist", teacherids);
		return deptRelationDao.deleteDeptRelation(map);
	}
	
	@Override
	public DeptRelationBean queryDeptRelationOne(Integer deptId,Integer teacherId){
		DeptRelation relation = null;
		if(deptId != null && teacherId != null){
			DeptRelationKey key = new DeptRelationKey();
			key.setDeptId(deptId);
			key.setTeacherId(teacherId);
			relation = deptRelationDao.selectDeptRelationOne(key);
		}
		return (DeptRelationBean) ObjectUtil.transfer(relation, DeptRelationBean.class);
	}
	
	@SuppressWarnings("unchecked")
	@Override
	public List<DeptRelationBean> queryDeptRelationList(DeptRelationBean bean){
		DeptRelationExample example = new DeptRelationExample();
		List<DeptRelation> relationlist = null;
		if(bean != null){
			if(bean.getDeptId() != null){
				example.createCriteria().andDeptIdEqualTo(bean.getDeptId());
			}
			if(bean.getTeacherId() != null){
				example.createCriteria().andTeacherIdEqualTo(bean.getTeacherId());
			}
			if(bean.getDeptUrl() != null){
				example.createCriteria().andDeptUrlLike(bean.getDeptUrl());
			}
			relationlist = deptRelationDao.selectDeptRelationList(example);
		}else{
			relationlist = deptRelationDao.selectDeptRelationList(null);
		}
		return ObjectUtil.transferList(relationlist, DeptRelationBean.class);
	}
}
